// ==UserScript==
// @name           Favorize
// @namespace      http://tyoro.orz.ne.jp/
// @description    色んな所にFavoritesの☆ボタンをつける。
// @include        http://twitter.1x1.jp/*
// @include        http://kichiku.oq.la/*
// @include        http://twwatcher.blog20.fc2.com/*
// @include        http://sho-ten.twitlife.jp/*
// @include        http://search.twitter.com/*
// @include        http://pcod.no-ip.org/yats/*
// @include        http://buzztter.com/ja/k/*
// @include        http://www.twitority.com/*
// @include        http://backtweets.com/*
// @include        http://twiple.jp/status/*
// @include        http://www.twingly.com/*
// @include        http://twitter.memol.us/*
// @include        http://meyou.jp/*
// ==/UserScript==

// author: tyoro ( http://tyoro.orz.ne.jp/exe/ )

(function(){
var domain_str = document.location.href.replace( /^http:\/\/([^\/]*).*/ig , "$1" );
var entry_x_path;
var id_x_path;
var id_reg;
var IE='\v'=='v';   /* {@@} */

switch(domain_str){
/*    case 'favotter.matope.com':
        entry_x_path = '//div[@class="hentry"]';
        id_x_path = 'self::div//a[@class="taggedlink"]';
        id_reg = /.*?id=(.*)/;
        break;*/
    case 'twitter.1x1.jp':
        entry_x_path = '//table[@class="list"]/tbody/tr/td[5]';
        id_x_path = 'self::td/a';
        id_reg = /.*\/(.*)/;
        break;
    case 'kichiku.oq.la':
        entry_x_path = '//div[@class="recentEntry"]';
        id_x_path = 'self::div/span[@class="usertext"]/a';
        id_reg = /.*\/(.*)/;
        break;
    case 'twwatcher.blog20.fc2.com':
        entry_x_path = '//div[@class="main"]/table/tbody/tr/td[3]';
        id_x_path = 'self::td/a';
        id_reg = /.*\/(.*)/;
        break;
    case 'sho-ten.twitlife.jp':
        entry_x_path = '//div[@class="date"]';
        id_x_path = 'self::div/a';
        id_reg = /.*\/(.*)/;
        break;
    case 'search.twitter.com':
        entry_x_path = '//div[@class="info"]';
        id_x_path = 'a[@class="lit"]';
        id_reg = /.*\/(.*)/;
        break;
    case 'pcod.no-ip.org':
     // entry_x_path = '//tr[@class="xfolkentry"]';
     // id_x_path = 'td/a[@class="taggedlink"]';
        
        entry_x_path = '//li[@class="hentry"]';                     /* @@ */
        id_x_path = 'div[@class="meta"]/span[@class="date"]/a';     /* @@ */
        
        id_reg = /.*\/(.*)/;
        break;
	case 'buzztter.com':
        entry_x_path = '//div[@class="status xfolkentry"]';
        id_x_path = 'a[@class="taggedlink"]';
        id_reg = /.*\/(.*)/;
        break;
        
    case 'www.twitority.com':                                       /* @@ */
        entry_x_path = '//div[@class="tweet"]/p[@class="info"]';    /* @@ */
        id_x_path = 'a[2]';                                         /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
        
    case 'backtweets.com':                                          /* @@ */
        entry_x_path = '//div[@class="timeline"]/p';                /* @@ */
        id_x_path = 'small/a';                                      /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
        
    case 'twiple.jp':                                               /* @@ */
        entry_x_path = '//div[@class="item twitter"]';              /* @@ */
        id_x_path = 'div[@class="content"]/span[@class="date"]/a';  /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
        
    case 'www.twingly.com':                                         /* @@ */
        entry_x_path = '//div[@class="info"]/p[@class="links"]';    /* @@ */
        id_x_path = 'a[2][contains(@href,"http://twitter.com/")]';  /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
        
    case 'twitter.memol.us':                                        /* @@ */
        entry_x_path = '//div[@class="tweet"]/div[@class="info"]';  /* @@ */
        id_x_path = 'a[3]';                                         /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
        
    case 'meyou.jp':                                                /* @@ */
        entry_x_path = '//p[@class="pt10"]/span[@class="time"]';    /* @@ */
        id_x_path = 'a';                                            /* @@ */
        id_reg = /.*\/(.*)/;                                        /* @@ */
        break;                                                      /* @@ */
}

//var path_to_api = 'http://twitter.com/favourings/create/';
var path_to_api = 'http://twitter.com/favorites/create/';   /* @@ */
    var formap = function(arr,callback,_index) {
        var _arr = [];
        for(var i = _index || 0, len = arr.length; i < len; i++) {
            _arr.push(callback(arr[i],i));
        }
        return _arr;
    };

    var index = 0;
    function mainloop(){
        var updates = $x(entry_x_path);
        if(!!updates) {
            try {
                formap(updates,addFavoritesButton,index);
                index = updates.length;
            } catch(e) {
             // alert("error"+e);
            }
        }
    }

    function addFavoritesButton(e) {
        var content = $x(id_x_path,e);
        if(content == null )
            return;
        var id = content[0].href.replace(id_reg,"$1");
        var starImg = document.createElement('img');
        starImg.src = 'http://assets2.twitter.com/images/icon_star_empty.gif';
        starImg.alt = 'Icon_star_empty';
        starImg.border = 0;
        starImg.id = 'status_star_'+id;

        var starLink = document.createElement('a');
        if (IE)                                                                             /* {@@} */
            starLink.attachEvent("on" + "click", function(){ addFavoritesStar(id); });      /* {@@} */
        else                                                                                /* {@@} */
            starLink.addEventListener('click', function(){ addFavoritesStar(id); } , false);
        
        starLink.setAttribute('style','cursor:pointer;');
        starLink.appendChild(starImg);
        e.appendChild(starLink);

        return;
    }

    addFilter(mainloop);
    mainloop();

    function addFilter(filter, i) {
        i = i || 4;
        if (window.AutoPagerize && window.AutoPagerize.addFilter) {
            window.AutoPagerize.addFilter(filter);
        }
        else if (i > 1) {
            setTimeout(arguments.callee, 1000, filter, i - 1);
        }
    }

    // cho45 - http://lowreal.net/
    function $x(exp, context) {
    
        if (!context) context = document;
        
        var resolver;
        var _exp;
        if (!IE) {
            resolver = function (prefix) {
                var o = document.createNSResolver(context)(prefix);
                return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml";
            }
            _exp = document.createExpression(exp, resolver);
        }
        var result = !IE ? _exp.evaluate(context, XPathResult.ANY_TYPE, null)
                         : document.evaluate(exp, context, null, XPathResult.ANY_TYPE, null);
        
        switch (result.resultType) {
            case XPathResult.STRING_TYPE : return result.stringValue;
            case XPathResult.NUMBER_TYPE : return result.numberValue;
            case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
            case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
                result = !IE ? _exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
                             : document.evaluate(exp, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);                
                var ret = [];
                for (var i = 0, len = result.snapshotLength; i < len ; i++) {
                    ret.push(result.snapshotItem(i));
                }
                
                return len != 0 ? ret : null;
            }
        }
        return null;
    }


function addFavoritesStar(id) {
    var img = document.getElementById('status_star_'+id);                   /* @@ */
    var api = path_to_api + id + '.xml';
    var callback = function(res){
            if(res.status == 200 ){
                img.src='http://twitter.com/images/icon_star_full.gif';     /* @@ */
            }else{
                img.src='http://twitter.com/images/icon_star_empty.gif';    /* @@ */
            }
            img.height = 16;                                                /* @@ */
            img.width  = 16;                                                /* @@ */
        };

    GM_xmlhttpRequest({
        method: 'POST',
        url: api,
        onload: callback
    });

    img.src='http://twitter.com/images/icon_throbber.gif';                  /* @@ */

}

})();